//
//  SignInController.m
//  HarmonizePub
//
//  Created by rzhy on 2017/1/3.
//  Copyright © 2017年 Zy. All rights reserved.
//

#import "SignInController.h"
#import "Config.h"
#import <YYModel.h>
#import <UIScrollView+EmptyDataSet.h>
#import "SignIn.h"
#import "SignInTableViewCell.h"

static NSString *CellIdentifier = @"Cell";

@interface SignInController () <SignInTableViewCellDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
{
    NSArray *_dataArray;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation SignInController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"到院签到";
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    UINib *nib = [UINib nibWithNibName:[SignInTableViewCell registerName] bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
    [self getMyConfirmList];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 获取个人可签到列表
- (void)getMyConfirmList {
    NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
    [HTTPSTool POSTRequestWithPath:XHGetMyConfirmList cookie:cookie params:nil success:^(NSDictionary * _Nonnull JSON) {
//        XHLog(@"可签到列表 -> JSON: %@", JSON);
        _dataArray = JSON[@"data"][@"list"];
        [_tableView reloadData];
    }];
}

#pragma mark - tableView delegate/dataSource Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [SignInTableViewCell cellHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SignInTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.delegate = self;
    cell.tableView = tableView;
    NSDictionary *dict = _dataArray[indexPath.row];
    SignIn *sign = [SignIn yy_modelWithDictionary:dict];
    cell.signIn = sign;
    return cell;
}

- (void)tableViewCell:(SignInTableViewCell *)ableViewCell didSignIn:(SignIn *)signIn indexPath:(NSIndexPath *)indexPath {
//    XHLog(@"signIn: %@, indexPath: %@", signIn, indexPath);
    NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
    NSDictionary *param = @{@"id" : signIn.identifier};
    [HTTPSTool POSTRequestWithPath:XHUserConfirmOrder cookie:cookie params:param success:^(NSDictionary * _Nonnull JSON) {
//        XHLog(@"签到 -> JSON: %@", JSON);
        NSMutableArray *arrayM = [_dataArray mutableCopy];
        [arrayM removeObjectAtIndex:indexPath.row];
        _dataArray = [arrayM copy];
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }];
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

#pragma mark - DZNEmptyDataSetSource Method
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"LoadFailure.png"];
    return image;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *string = XHEmptyDes;
    NSAttributedString *abs = [[NSAttributedString alloc] initWithString:string attributes:nil];
    return abs;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -64.0f;
}

#pragma mark - DZNEmptyDataSetDelegate Method
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    [self getMyConfirmList];
}

@end
